<template>
  <div class="news-detail">
    <div class="page-header">
      <div class="container">
        <h1 class="page-title">{{ pageInfo.pageTitle }}</h1>
        <h3 class="page-subtitle">{{ pageInfo.pageSubTitle }}</h3>
      </div>
    </div>

    <div class="container">
      <div class="content-section">
        <!-- 新闻内容 -->
        <div class="news-content">
          <!-- 新闻标题 -->
          <h1 class="news-title">{{ pageInfo.title }}</h1>

          <!-- 新闻内容 -->
          <div class="news-body">
            <div v-html="pageInfo.content" class="news-full-content"></div>
          </div>

          <!-- 底部返回按钮 -->
          <div class="news-actions">
            <el-button @click="goBack" type="primary" size="large">
              <el-icon><ArrowLeft /></el-icon>
              返回列表
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { subpageApi } from "@/api/user";
import { ElMessage } from "element-plus";
import { ArrowLeft, Calendar, Collection } from "@element-plus/icons-vue";

export default {
  name: "NewsDetails",
  components: {
    ArrowLeft,
    Calendar,
    Collection,
  },
  computed: {
    pageTitle() {
      return this.$route.query.title;
    },
  },
  setup() {
    const route = useRoute();
    const router = useRouter();

    // 状态管理
    const loading = ref(true);
    const newsData = ref(null);

    // 解析富文本内容
    const parseContent = (content) => {
      if (!content)
        return { content: "", excerpt: "", image: "", date: "", category: "" };

      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return {
          content: content,
          excerpt: "",
          image: "",
          date: "",
          category: "",
        };
      } catch (e) {
        return {
          content: content,
          excerpt: "",
          image: "",
          date: "",
          category: "",
        };
      }
    };

    // 返回上一页
    const goBack = () => {
      router.go(-1);
    };

    const pageInfo = ref({
      pageTitle: "",
      content: "",
    });
    const getDetails = () => {
      const sessionItem = sessionStorage.getItem("activityDetails");
      const details = JSON.parse(sessionItem);
      pageInfo.value = {
        ...details,
        content: parseContent(details.content)?.content || "",
      };
      console.log(pageInfo.value);

      loading.value = false;
    };

    // 组件挂载时加载数据
    onMounted(() => {
      getDetails();
    });

    return {
      loading,
      newsData,
      goBack,
      pageInfo,
    };
  },
};
</script>

<style scoped>
.news-detail {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  background: linear-gradient(
    15deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
  backdrop-filter: blur(10px);
  /* padding: 160px 0 60px 0; */
  height: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: white;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.content-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.loading-container {
  padding: 2rem 0;
}

.empty-container {
  padding: 4rem 0;
  text-align: center;
}

.news-title {
  font-size: 2.5rem;
  color: #333;
  margin-bottom: 1.5rem;
  line-height: 1.3;
  text-align: center;
}

.news-meta {
  display: flex;
  justify-content: center;
  gap: 2rem;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.news-date,
.news-category {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #666;
  font-size: 1rem;
}

.news-category {
  background: #667eea;
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-size: 0.9rem;
}

.news-image-section {
  margin: 2rem 0;
  text-align: center;
}

.news-image {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.news-body {
  margin: 2rem 0;
}

.news-full-content {
  color: #333;
  line-height: 1.8;
  font-size: 1.1rem;
}
:deep(.resizable-image) {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
}

/* 富文本内容样式 */
.news-full-content :deep(h1),
.news-full-content :deep(h2),
.news-full-content :deep(h3),
.news-full-content :deep(h4) {
  color: #333;
  margin: 1.5rem 0 1rem 0;
  font-weight: 600;
}

.news-full-content :deep(p) {
  margin-bottom: 1rem;
  text-align: justify;
}

.news-full-content :deep(ul),
.news-full-content :deep(ol) {
  margin: 1rem 0;
  padding-left: 2rem;
}

.news-full-content :deep(li) {
  margin-bottom: 0.5rem;
}

.news-full-content :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.5rem auto;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.news-full-content :deep(blockquote) {
  border-left: 4px solid #667eea;
  padding-left: 1rem;
  margin: 1.5rem 0;
  color: #666;
  font-style: italic;
}

.news-actions {
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 1px solid #eee;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 1rem;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }

  .content-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .news-title {
    font-size: 1.8rem;
  }

  .news-meta {
    flex-direction: column;
    gap: 1rem;
    align-items: center;
  }

  .news-actions {
    flex-direction: column;
    align-items: center;
  }
}
</style>
